app.controller('home', function ($scope, $http) {

    $scope.keyWord = '';
    $scope.page = 1;
    $scope.allPage='';
    $scope.getClassList=function () {
        //页面索引值
        sessionStorage.setItem('pageIndex', 1);
        var infiniteScrollPreloader = $('.infinite-scroll-preloader');
        // 显示加载提示符
        infiniteScrollPreloader.show();
        $http({
            method: 'get',
            url: app.netPath + '/class/classlatest',
            params: {page:$scope.page}
        }).then(function (res) {
            // 删除加载提示符
            infiniteScrollPreloader.hide();
            console.log(res.data);
            if (!res.data.error) {
                //换一批显示
                $("#nextPage").css('display','block');
                $scope.classInfo = res.data.data;
                $scope.allPage=res.data.allPage;
                for (var i = 0; i < $scope.classInfo.length ; i++) {
                    if($scope.classInfo[i].classImg){
                        $scope.classInfo[i].classImg = app.netPath + '/resources/classImg/' + $scope.classInfo[i].classImg;
                        app.buildImageUrl($scope.classInfo[i],'classImg',function () {
                            $scope.$digest();
                        });
                    }
                    //设置星星
                    $scope.postStar = [];
                    for (var j = 0; j < $scope.classInfo[i].classEvaluateGrade; j++) {
                        var postStar = '';
                        $scope.postStar.push(postStar);
                    }
                    $scope.classInfo[i].postStar = $scope.postStar;

                }


                console.log($scope.classInfo[0])
            }
            else {
                $.toast(res.data.msg)
            }
        },function(){
            $.toast('网络走丢了！')
        })
    };
    //检索课程
    $scope.searchClass = function () {
        var keyWord=$scope.keyWord;
        if (keyWord != null && keyWord != '') {
            sessionStorage.setItem('keyWord', keyWord);
            location.href = '#search'
        }else {

        }
    };
    //跳转课程详情
    $scope.getClassId = function (classId) {
        sessionStorage.setItem('classId', classId);
        location.href = '#xiangqing';
    };

    //换一批
    $scope.getNextPage=function(){
        console.log($scope.allPage+">>>");
        if($scope.page<$scope.allPage){
            $scope.page++;
            $scope.classInfo=[];
            //换一批隐藏
            $("#nextPage").css('display','none');
            console.log($scope.page);
            //$location.hash('top');
            //
            //// 调用 $anchorScroll()
            //$anchorScroll().yOffset=500;
            $scope.getClassList();

        }
       else{
            $scope.page=1;
            $scope.getClassList();
        }
    }

});